package sample.servlet;

import fri.entiry.FrUser;
import fri.entiry.FrHistory;
import fri.businessLayer.UserBeanRemote;
import fri.businessLayer.HistoryBeanRemote;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class HistoryController extends HttpServlet {

    @EJB
    HistoryBeanRemote historyBeanRemote;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            HttpSession session = request.getSession(true);
            if (session.getAttribute("STATUS_LOGIN") != null) {
                if ((Integer)session.getAttribute("STATUS_LOGIN") == 1) {
                    FrUser frUser =  (FrUser)session.getAttribute("USER_LOGIN");
                    List<FrHistory> lstFrHistory = historyBeanRemote.getHistory(frUser.getUserId());
                    request.setAttribute("lstFrHistory", lstFrHistory);
                    RequestDispatcher rd = request.getRequestDispatcher("reqHistory.jsp");
                    rd.forward(request, response);
                }
            }
            RequestDispatcher rd = request.getRequestDispatcher("reqLogin.jsp");
            rd.forward(request, response);
        } finally {
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
